package com.viphuli.app.tool.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import butterknife.OnClick;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.page.ContactBean;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.utils.PinYin;

/**
 * 添加成员
 * 
 * @author li.li
 *
 */
public class CircleUserAddFragment extends BaseProgressFragment {
	private AsyncQueryHandler asyncQueryHandler; // 异步查询数据库类对象
	@Override
	protected int obtainContentRes() {
		return R.layout.frag_circle_user_add;
	}

	@Override
	protected void initParams() {
		//解析参数
		Bundle args = getArguments();
	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@OnClick(R.id.id_user_add_wechat)
	public void wechatAdd() {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {

			CircleUserAddWechatFragment.go(getActivity(), token.getUser().circleId());

		}
	}

	@OnClick(R.id.id_user_add_manual)
	public void manualAdd() {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {
			Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人Uri；
			// 查询的字段
			String[] projection = {
					ContactsContract.CommonDataKinds.Phone._ID,
					ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
					ContactsContract.CommonDataKinds.Phone.DATA1,
					"sort_key",
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
					ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
					ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };
			// 按照sort_key升序查詢
			asyncQueryHandler = new MyAsyncQueryHandler(
					caller.getContentResolver());
			asyncQueryHandler.startQuery(0, null, uri, projection, null,
					null, "sort_key COLLATE LOCALIZED asc");

		}

	}
	private class MyAsyncQueryHandler extends AsyncQueryHandler {

		public MyAsyncQueryHandler(ContentResolver cr) {
			super(cr);
		}

		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			if (cursor != null && cursor.getCount() > 0) {
				ArrayList<ContactBean> list = new ArrayList<ContactBean>();
				cursor.moveToFirst(); // 游标移动到第一项
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					String name = cursor.getString(1);
					String number = cursor.getString(2);
					// String sortKey = cursor.getString(3);
					// int contactId = cursor.getInt(4);
					// Long photoId = cursor.getLong(5);
					// String lookUpKey = cursor.getString(6);

					ContactBean contact = new ContactBean();
					contact.setRealName(name);
					contact.setPhone(number.replace("-", ""));
					contact.setSpellName(PinYin.getPinYin(contact.getRealName()));
					list.add(contact);
				}
				if (list.size() > 0) {
					AccessTokenKeeper tokenUser = AccessTokenKeeper.readAccessToken(getActivity());
					CircleUserAddManualFragment.go(getActivity(), tokenUser.getUser().circleId(),list);
				} else {
					ViewUtils.toast(R.string.no_contact_friend);
				}
			} else {
				ViewUtils.toast(R.string.no_contact_friend);
			}
			super.onQueryComplete(token, cookie, cursor);
		}
	}
	public static void go(Activity act) {
		MyPageHelper.circleUserAdd.showMyPage(act);
	}

	public static void go(Activity act, int circleId) {

		Bundle args = new Bundle();
		args.putInt("circle_id", circleId);
		MyPageHelper.circleUserAdd.showMyPage(act, args);

	}

}
